﻿
Public Class croppingimage
    Shared crsx As Integer
    Shared crsy As Integer
    Shared rect As Rectangle
    Shared cropwidth As Integer
    Shared cropheight As Integer
    Public Shared Sub Start(PictureBoxToCropFrom As PictureBox, CroppingForm As Form, ImgLocation As String, CropPen As Pen)

        ' This call is required by the designer.
        ' Add any initialization after the InitializeComponent() call.
        CroppingForm.Width = Screen.PrimaryScreen.WorkingArea.Width
        CroppingForm.Height = Screen.PrimaryScreen.WorkingArea.Height
        CroppingForm.WindowState = FormWindowState.Maximized
        Dim ht As Double
        Dim wt As Double
        PictureBoxToCropFrom.Height = CroppingForm.Height
        Dim aspectratio As Double = PictureBoxToCropFrom.Image.Width / PictureBoxToCropFrom.Image.Height
        ht = PictureBoxToCropFrom.Height
        wt = Convert.ToDouble(ht * aspectratio)
        PictureBoxToCropFrom.Size = New Size(wt, ht)
        Dim nb As New Bitmap(Image.FromFile(ImgLocation), wt, ht)
        Dim bmp As New Bitmap(PictureBoxToCropFrom.Image, Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height)
        '  PictureBoxToCropFrom.Image = bmp
    End Sub
    ''' <summary>
    ''' This method is need to be called from the "yourpicturebox_mousedown" event.
    ''' </summary>
    ''' Put here your "e" parameter from the yourpicturebox_mousedown sub.
    ''' <param name="e"></param>
    ''' The picturebox with the image to crop loaded.
    ''' <param name="PictureBoxToCropFrom"></param>
    ''' The pen to mark the cropping area with.
    ''' <param name="CropPen"></param>
    Public Shared Sub WhenMouseIsDown(ByVal e As System.Windows.Forms.MouseEventArgs, PictureBoxToCropFrom As PictureBox, CropPen As Pen)
        Try

            If e.Button = Windows.Forms.MouseButtons.Left Then

                crsx = e.X
                crsy = e.Y

                PictureBoxToCropFrom.Cursor = Cursors.Cross

            End If
            PictureBoxToCropFrom.Refresh()
        Catch exc As Exception
        End Try
    End Sub
    Dim tmppoint As Point
    ''' <summary>
    ''' This method is need to be called from the "yourpicturebox_mousemove" event.
    ''' </summary>
    ''' Put here your "e" parameter from the yourpicturebox_mousemove sub.
    ''' <param name="e"></param>
    ''' The picturebox with the image to crop loaded.
    ''' <param name="PictureBoxToCropFrom"></param>
    ''' The pen to mark the cropping area with.
    ''' <param name="CropPen"></param>
    Public Shared Sub WhenMouseMoves(e As System.Windows.Forms.MouseEventArgs, PictureBoxToCropFrom As PictureBox, CropPen As Pen)

        Try

            If PictureBoxToCropFrom.Image Is Nothing Then Exit Sub

            If e.Button = Windows.Forms.MouseButtons.Left Then

                PictureBoxToCropFrom.Refresh()
                cropwidth = e.X - crsx
                cropheight = e.Y - crsy
                rect = New Rectangle(crsx, crsy, cropwidth, cropheight)
                PictureBoxToCropFrom.CreateGraphics.DrawRectangle(CropPen, rect)
            End If
            ' GC.Collect()

        Catch exc As Exception

            If Err.Number = 5 Then Exit Sub
        End Try
    End Sub
    ''' <summary>
    ''' This method is need to be called from the "yourpicturebox_mouseup" event.
    ''' </summary>
    ''' The picturebox with the image to crop loaded.
    ''' <param name="PictureBoxToCropFrom"></param>
    Public Shared Sub WhenMouseIsUp(PictureBoxToCropFrom As PictureBox)

        Try
            PictureBoxToCropFrom.Cursor = Cursors.Default
            Try


                If cropwidth < 1 Then
                    Exit Sub
                End If

                Dim rect As Rectangle = New Rectangle(crsx, crsy, cropwidth, cropheight)
                Dim bit As Bitmap = New Bitmap(PictureBoxToCropFrom.Image, PictureBoxToCropFrom.Width, PictureBoxToCropFrom.Height)

                Dim cropBitmap = New Bitmap(cropwidth, cropheight)
                Dim g As Graphics = Graphics.FromImage(cropBitmap)
                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                g.CompositingQuality = Drawing2D.CompositingQuality.HighQuality
                g.DrawImage(bit, 0, 0, rect, GraphicsUnit.Pixel)
                PictureBoxToCropFrom.Image = cropBitmap

                PictureBoxToCropFrom.Visible = False
                PictureBoxToCropFrom.Visible = True
                PictureBoxToCropFrom.SizeMode = PictureBoxSizeMode.Zoom
                cropBitmap = Nothing
                bit = Nothing
                rect = Nothing

                RIS(PictureBoxToCropFrom)
            Catch exc As Exception
            End Try
        Catch exc As Exception
        End Try

    End Sub
    Shared Sub RIS(PictureBoxToCropFrom As PictureBox)

        If PictureBoxToCropFrom.Image.Size.Height > PictureBoxToCropFrom.Size.Height Then
            PictureBoxToCropFrom.SizeMode = PictureBoxSizeMode.Zoom
        Else
            If PictureBoxToCropFrom.Image.Size.Width > PictureBoxToCropFrom.Size.Width Then
                PictureBoxToCropFrom.SizeMode = PictureBoxSizeMode.Zoom
            Else
                PictureBoxToCropFrom.SizeMode = PictureBoxSizeMode.CenterImage
            End If
        End If
    End Sub
End Class